package com.icen.utils.comm;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.View;

/**
 * Dialog Utils Class
 * 
 * @author IcenHan
 * 
 */
public class DialogUtils {

	public static ProgressDialog showProgressDialog(Context context,
			String message) {
		return showProgressDialog(context, null, message);
	}

	public static ProgressDialog showProgressDialog(Context context,
			String message, boolean isCancelabledOnTouch) {
		return showProgressDialog(context, null, null, message, false,
				isCancelabledOnTouch, null);
	}

	public static ProgressDialog showProgressDialog(Context context,
			String title, String message) {
		return ProgressDialog.show(context, title, message);
	}

	public static ProgressDialog showProgressDialog(Context context,
			Drawable icon, String title, String message) {
		return showProgressDialog(context, icon, title, message, false, false,
				null);
	}

	private static ProgressDialog showProgressDialog(Context context,
			Drawable icon, String title, String message, boolean isCancelable,
			boolean isCancelabledOnTouch, View view) {
		ProgressDialog progressDialog = new ProgressDialog(context);

		if (icon != null) {
			progressDialog.setIcon(icon);
		}

		if (title != null) {
			progressDialog.setTitle(title);
		}

		if (message != null) {
			progressDialog.setMessage(message);
		}

		if (!isCancelable) {
			progressDialog.setCancelable(isCancelable);
		}

		if (!isCancelabledOnTouch) {
			progressDialog.setCanceledOnTouchOutside(isCancelabledOnTouch);
		}

		if (view != null) {
			progressDialog.setView(view);
		}

		progressDialog.show();

		return progressDialog;
	}

	public static void showAlertDialog(Context context, String Message,
			String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonOnClickListener,
			String negativeButtonText,
			DialogInterface.OnClickListener negativeButtonOnClickListener) {
		showAlertDialog(context, null, null, Message, null, null, null, null,
				null, null, 0, null, null, positiveButtonText,
				positiveButtonOnClickListener, null, null, negativeButtonText,
				negativeButtonOnClickListener);
	}

	public static void showAlertDialog(Context context, String title,
			String message, String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonOnClickListener,
			String negativeButtonText,
			DialogInterface.OnClickListener negativeButtonOnClickListener) {
		showAlertDialog(context, null, title, message, null, null, null, null,
				null, null, 0, null, null, positiveButtonText,
				positiveButtonOnClickListener, null, null, negativeButtonText,
				negativeButtonOnClickListener);

	}

	public static void showAlertDialog(Context context, Drawable icon,
			String title, String message, String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonOnClickListener,
			String negativeButtonText,
			DialogInterface.OnClickListener negativeButtonOnClickListener) {
		showAlertDialog(context, icon, title, message, null, null, null, null,
				null, null, 0, null, null, positiveButtonText,
				positiveButtonOnClickListener, null, null, negativeButtonText,
				negativeButtonOnClickListener);

	}

	public static void showSingleChoiceAlertDialog(Context context,
			String title, String[] singleChoiceItems, int checkedItem,
			DialogInterface.OnClickListener singleChoiceListener) {
		showAlertDialog(context, null, title, null, null, null, null, null,
				null, singleChoiceItems, checkedItem, singleChoiceListener,
				null, null, null, null, null, null, null);
	}

	public static void showMultiChoiceAlertDialog(
			Context context,
			String title,
			String[] multiChoiceItems,
			boolean[] checkedItems,
			DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener,
			String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonOnClickListener) {
		showAlertDialog(context, null, title, null, null, null,
				multiChoiceItems, checkedItems, multiChoiceClickListener, null,
				0, null, null, positiveButtonText,
				positiveButtonOnClickListener, null, null, null, null);
	}

	private static void showAlertDialog(
			Context context,
			Drawable icon,
			String title,
			String message,
			String[] items,
			DialogInterface.OnClickListener itemsListener,
			String[] multiChoiceItems,
			boolean[] checkedItems,
			DialogInterface.OnMultiChoiceClickListener multiChoiceClickListener,
			String[] singleChoiceItems, int checkedItem,
			DialogInterface.OnClickListener singleChoiceListener, View view,
			String positiveButtonText,
			DialogInterface.OnClickListener positiveButtonOnClickListener,
			String neutralButtonText,
			DialogInterface.OnClickListener neutralButtonOnClickListener,
			String negativeButtonText,
			DialogInterface.OnClickListener negativeButtonOnClickListener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);

		if (icon != null) {
			builder.setIcon(icon);
		}

		if (title != null) {
			builder.setTitle(title);
		}

		if (message != null) {
			builder.setMessage(message);
		}

		if (items != null && itemsListener != null) {
			builder.setItems(items, itemsListener);
		}

		if (multiChoiceItems != null && multiChoiceClickListener != null) {
			builder.setMultiChoiceItems(multiChoiceItems, checkedItems,
					multiChoiceClickListener);
		}

		if (singleChoiceItems != null && singleChoiceListener != null) {
			builder.setSingleChoiceItems(singleChoiceItems, checkedItem,
					singleChoiceListener);
		}

		if (view != null) {
			builder.setView(view);
		}

		if (positiveButtonText != null || positiveButtonOnClickListener != null) {
			builder.setPositiveButton(positiveButtonText,
					positiveButtonOnClickListener);
		}

		if (neutralButtonText != null || neutralButtonOnClickListener != null) {
			builder.setNeutralButton(neutralButtonText,
					neutralButtonOnClickListener);
		}

		if (negativeButtonText != null || negativeButtonOnClickListener != null) {
			builder.setNegativeButton(negativeButtonText,
					negativeButtonOnClickListener);
		}

		builder.create().show();
	}
}
